<template>
  <div>
    <el-dialog
      center
      title="会员编辑"
      :visible.sync="dialogInfo.isShow"
      :close-on-press-escape="false"
      :before-close="cancel"
    >
      <el-form :model="member_info" :rules="rules" ref="userinfo">
        <el-form-item
          prop="phone"
          label="手机号"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="member_info.phone"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="nickname"
          label="昵称"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="member_info.nickname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          label="密码"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="member_info.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="member_info.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="comfirm">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { memberinfo, memberedit } from "../../request/api";
//引入辅助性函数
import { mapActions } from "vuex";
export default {
  props: ["dialogInfo"],
  data() {
    return {
      member_info: {
        nickname: "", //会员昵称名称
        phone: "", //会员手机号
        password: "", //会员密码
        status: 1, //状态   1正常2禁用   number类型
      },
      formLabelWidth: "120px",
      rules: {
        phone: [
          {
            required: true,
            message: "请输入您要修改的手机号",
            trigger: "blur",
          },
          {
            min: 11,
            max: 11,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        nickname: [
          { required: true, message: "请输入您要修改的昵称", trigger: "blur" },
          { min: 2, max: 4, message: "长度在 2 到 4 个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {},
  computed: {},
  methods: {
    ...mapActions({
      getMemberAction: "member/getMemberAction",
    }),

    reset() {
      this.member_info = {
        nickname: "", //会员昵称名称
        phone: "", //会员手机号
        password: "", //会员密码
        status: 1, //状态   1正常2禁用   number类型
      };
    },
    //封装一个取消事件
    cancel() {
      //去改变父组件的isShow的状态
      this.$emit("cancel", false);
      //调取清空事件
      this.reset();
    },
    //封装一个函数用来数据回显
    lookup(uid) {
      //调用一条数据查询
      memberinfo({
        uid,
      }).then((res) => {
        if (res.code == 200) {
          this.member_info = res.list;
          this.member_info.uid = uid;
        }
      });
    },
    //封装一个修改事件
    comfirm() {
      this.$refs.userinfo.validate((valid) => {
        if (valid) {
          memberedit(this.member_info).then((res) => {
            if(res.code == 200){
              this.$message.success(res.msg);
              //关闭弹框
              this.cancel();
              //调用列表接口
              this.getMemberAction();

            }else{
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped>
</style>